package com.shiguo365.market.receiver;

import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;

import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.display.activity.MainActivity;
import com.shiguo365.market.display.util.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by caomenglong on 16/8/19.
 */
public class JPushReceiver extends BroadcastReceiver {
    private final String TAG = "JPushReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.d(TAG, "onReceive - " + intent.getAction());

        //收到了通知
        if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            String content = bundle.getString(JPushInterface.EXTRA_ALERT);
            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            String type = bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE);

            //ToastUtils.with(MyApplication.getAppContext()).show("收到了通知"+"type:"+type+" id:"+String.valueOf(notificationId)+" 标题："+title+" context:"+content+" extras:"+extras);
            //Log.d(TAG, "收到了通知" + "type:" + type + " id:" + String.valueOf(notificationId) + " 标题：" + title + " context:" + content + " extras:" + extras);

            Intent intent1 = new Intent("com.shiguo365.com.intent.NOTIFICATION_RECEIVED");
            intent1.putExtra("title", title);
            intent1.putExtra("content", content);
            intent1.putExtra("extras", extras);
            intent1.putExtra("notificationId", notificationId);
            context.sendBroadcast(intent1);


            SQLiteDatabase db =MyApplication.getDbHelper().getWritableDatabase();
            ContentValues values =new ContentValues();
            values.put("notificationId",notificationId);
            values.put("title",title);
            values.put("content",content);
            values.put("extras",extras);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time=sdf.format(new Date());
            values.put("creatTime",time);
            db.insert("Notification",null,values);
            db.close();

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            //打开了通知
            //ToastUtils.with(MyApplication.getAppContext()).show("收到了通知");
            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            String content = bundle.getString(JPushInterface.EXTRA_ALERT);
            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            //Log.d(TAG, "点击了通知" + "id:" + String.valueOf(notificationId) + " 标题：" + title + " context:" + content + " extras:" + extras);
            Intent intent2 = new Intent(context, MainActivity.class);
            intent2.putExtra("title", title);
            intent2.putExtra("content", content);
            intent2.putExtra("extras", extras);
            intent2.putExtra("notificationId", notificationId);
            intent2.putExtra("FromACTION_NOTIFICATION_OPENED", true);
            intent2.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent2);
        }


    }
}
